using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Documents;

namespace com.bodurov.SilverlightControls.XmlCodeEditor.Extensions
{
    public static class InlineCollection_Ext
    {
        public static string GetContent(this InlineCollection collection)
        {
            var sb = new StringBuilder();
            foreach (var inline in collection)
            {
                var run = inline as Run;
                if(run != null)
                {
                    sb.Append(run.Text);
                    continue;
                }
                var subSpan = inline as Span;
                if(subSpan != null)
                {
                    sb.Append(subSpan.Inlines.GetContent());
                    continue;
                }
                var lineBreak = inline as LineBreak;
                if(lineBreak != null)
                {
                    sb.AppendLine();
                    continue;
                }
            }
            return sb.ToString();
        }

        public static string GetWhiteSpaceOnTheLeft(this InlineCollection collection)
        {
            var sb = new StringBuilder();
            foreach (var inline in collection)
            {
                var run = inline as Run;
                if (run != null)
                {
                    var str = run.Text;
                    foreach (var ch in str)
                    {
                        if (Char.IsWhiteSpace(ch))
                            sb.Append(ch);
                        else
                            return sb.ToString();
                    }
                    continue;
                }
                var subSpan = inline as Span;
                if (subSpan != null)
                {
                    sb.Append(subSpan.Inlines.GetWhiteSpaceOnTheLeft());
                    continue;
                }
            }
            return sb.ToString();
        }


        public static IEnumerable<Run> GetRuns(this InlineCollection collection)
        {
            foreach (var inline in collection)
            {
                var run = inline as Run;
                if (run != null)
                {
                    yield return run;
                }
                var span = inline as Span;
                if(span != null)
                {
                    foreach(var subRun in span.Inlines.GetRuns())
                    {
                        yield return subRun;
                    }
                }
            }
        }

        public static Run GetLastRun(this InlineCollection collection)
        {
            for(var i = collection.Count - 1; i >= 0; i--)
            {
                var inline = collection[i];
                var run = inline as Run;
                if (run != null)
                {
                    return run;
                }
                var span = inline as Span;
                if (span != null)
                {
                    var subRun = span.Inlines.GetLastRun();
                    if (subRun != null)
                    {
                        return subRun;
                    }
                }
            }
            return null;
        }
    }
}